﻿using DevExpress.XtraEditors.Repository;
using DevExpress.XtraTreeList;
using DevExpress.XtraTreeList.Nodes;
using HuaCheng.CommonClass;
using HuaCheng.Controller;
using HuaCheng.DataAccess;
using HuaCheng.Model;
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace HuaCheng.Views
{
    /// <summary>
    /// 工步跳转
    /// </summary>
    public partial class StepJump : Form
    {
        //定义单元格模板
        RepositoryItem TextNull = new RepositoryItemTextEdit();
        //定义当前通道工步集合
        List<StepInfo> currentList = new List<StepInfo>();
        //定义单位对象
        public UnitOrder uOrder;
        //从表中读取工步数据
        List<byte[]> byteList;
        //正在运行的工步号
        public int stepNum;
        //想要跳转的工步号
        public int jumpNum;
        //判断用户点击的是确定还是取消
        public bool Result;
        public StepJump(string testId,string ip, int gid, int mid,int cid,int stepNum)
        {
            InitializeComponent();
            InitUnit();//初始化单位
            InitParams(testId,ip, gid, mid,cid);//初始化获得表的数据源          
            LoadStepInfo(ip, gid, cid,stepNum);//把数据源赋给下面的表控件          
        }
        /// <summary>
        /// 从配置文件读取单位
        /// </summary>
        public void InitUnit()
        {
            uOrder = UnitOrderControl.GetUnit();
            //this.tLStepTime.Caption = "工步时间(" + uOrder.StepEditTime + ")";
            //this.tLVoltage.Caption = "电压(" + uOrder.Voltage + ")";
            //this.tLCurrent.Caption = "电流(" + uOrder.Current + ")";
            //this.tLCap.Caption = "充电容量(" + uOrder.Capacity + ")";
            //this.tLEnergy.Caption = "充电能量(" + uOrder.Energy + ")";
            this.tLDeltaVolt.Caption = "-ΔV(" + uOrder.Voltage + ")";
            //this.tLPower.Caption = "功率(" + uOrder.Power + ")";
            //this.tLLoadResistor.Caption = "负载(" + uOrder.Resistance + ")";
            //this.tLCutoffCurr.Caption = "截止电流(" + uOrder.Current + ")";
            this.tLMaxVi.Caption = "MaxVi(" + uOrder.Voltage + ")";
            this.tLMinVi.Caption = "MinVi(" + uOrder.Voltage + ")";
            this.tLMaxTi.Caption = "MaxTi(" + uOrder.Template + ")";
            this.tLMinTi.Caption = "MinTi(" + uOrder.Template + ")";
            //this.tLSingleVolt.Caption = "单体恒压电压(" + uOrder.Voltage + ")";
        }
        /// <summary>
        /// 读取工步表中数据和测试表数据
        /// </summary>
        /// <param name="ip"></param>
        /// <param name="gid"></param>
        /// <param name="cid"></param>
        public void InitParams(string testid,string ip, int gid, int mid,int cid)
        {
            try //从表中读取工步数据
            {
                byteList = StepHelper.GetBinaryListById(testid,ip, gid,mid, cid);
                if (byteList.Count > 0)
                {
                    List<StepInfo> loadStepList = new List<StepInfo>();
                    //将工步数据恢复成StepInfo对象
                    for (int i = 0; i < byteList.Count; i++)
                    {
                        //将字节数组反序列化StepInfo对象
                        StepInfo stepInfo = (StepInfo)SerializableControl.DeserializeWithBinary(byteList[i]);
                        loadStepList.Add(stepInfo);
                    }
                    //将工步list转为当前单位的数值
                    currentList = DataControl.DataConvertToCurrent(loadStepList);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message + e.StackTrace);
            }
        }
        private void tLStepJump_CustomNodeCellEdit(object sender, GetCustomNodeCellEditEventArgs e)
        {
            e.RepositoryItem = TextNull;
        }
        /// <summary>
        /// 根据工步表初始化工步内容
        /// </summary>
        /// <param name="gid"></param>
        /// <param name="cid"></param>
        /// <param name="devID"></param>
        private void LoadStepInfo(string ip, int gid, int cid,int stepNum)
        {
            if (currentList.Count > 0)//有工步信息才赋值
            {
                tLStepJump = StepControl.SetTreeListByStepList(currentList, tLStepJump);//把该通道工步表中的信息单位化以后显示在tLStepEditor
            }
            if (byteList.Count < 10)//如果不足10行则补上空行
            {
                for (int i = 0; i < 10 - byteList.Count; i++)
                {
                    TreeListNode tmpNode = tLStepJump.Nodes.Add();
                    tLStepJump.FocusedNode = tmpNode;
                }
            }
            tLStepJump.FocusedNode = tLStepJump.Nodes[stepNum-1];//聚焦到正在运行的工步
        }
        /// <summary>
        /// 起始工步的校验
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void nudJumpStep_Validating(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (nudJumpStep.Value < 1)
            {
                if (IndexPage.language == "en-US")
                {
                    MessageBox.Show("Start step id must bigger than or equal to 1");
                }
                else
                {
                    MessageBox.Show("跳转工步必须大于等于1");
                }
                nudJumpStep.Value = 1;
            }
            else if(nudJumpStep.Value == stepNum)
            {
                if (IndexPage.language == "en-US")
                {
                    MessageBox.Show("Can't jump to current step");
                }
                else
                {
                    MessageBox.Show("不能跳转到当前工步");
                }
                nudJumpStep.Value = 1;
            }
            else if (nudJumpStep.Value >= tLStepJump.Nodes.Count)
            {
                if (IndexPage.language == "en-US")
                {
                    MessageBox.Show("Start step id is larger than total steps ");
                }
                else
                {
                    MessageBox.Show("起始工步必须小于工步数");
                }
                nudJumpStep.Value = 1;
            }
        }
        /// <summary>
        /// 确定按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOk_Click(object sender, EventArgs e)
        {
            jumpNum = Convert.ToInt32(nudJumpStep.Value);
            if (jumpNum > byteList.Count)
            {
                MessageBox.Show("跳转工步值超过工步数！","提示",MessageBoxButtons.OK);
                nudJumpStep.Value = 1;
            }
            else
            {
                Result = true;
                this.Close();
            }         
        }
        /// <summary>
        /// 取消按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCancle_Click(object sender, EventArgs e)
        {
            Result = false;
            this.Close();
        }

        #region 国际化
        private void StepJump_Load(object sender, EventArgs e)
        {
            ResourceCulture.SetCurrentCulture(IndexPage.language);
            this.SetResourceCulture();
        }
        /// <summary>
        /// 为当前页面内所有控件设置文本语种
        /// </summary>
        private void SetResourceCulture()
        {
            this.Text = ResourceCulture.GetString("StepJump");
            this.lblJump.Text = ResourceCulture.GetString("JumpID");
            this.btnOk.Text = ResourceCulture.GetString("SureText");
            this.btnCancle.Text = ResourceCulture.GetString("LoginCancel");
            this.tLStepNum.Caption = ResourceCulture.GetString("StepID");
            this.tLStepName.Caption = ResourceCulture.GetString("StepName");
            this.tLStepTime.Caption = ResourceCulture.GetString("StepTime") + "(" + uOrder.StepEditTime + ")";
            this.tLVoltage.Caption = ResourceCulture.GetString("lblVoltageText") + "(" + uOrder.Voltage + ")";
            this.tLCurrent.Caption = ResourceCulture.GetString("lblCurrentText") + "(" + uOrder.Current + ")";
            this.tLCap.Caption = ResourceCulture.GetString("Capacity") + "(" + uOrder.Capacity + ")";
            this.tLEnergy.Caption = ResourceCulture.GetString("Energy") + "(" + uOrder.Energy + ")";
            this.tLPower.Caption = ResourceCulture.GetString("Power") + "(" + uOrder.Power + ")";
            this.tLLoadResistor.Caption = ResourceCulture.GetString("Resistance") + "(" + uOrder.Resistance + ")";
            this.tLCutoffCurr.Caption = ResourceCulture.GetString("EndCurrent") + "(" + uOrder.Current + ")";
            this.tLBeginValue.Caption = ResourceCulture.GetString("StartValue");
            this.tLSlope.Caption = ResourceCulture.GetString("Slope");
            this.tLEndValue.Caption = ResourceCulture.GetString("EndValue");
            this.tLAmplitude1.Caption = ResourceCulture.GetString("Amplitude") + " 1";
            this.tLTime1.Caption = ResourceCulture.GetString("Time") + " 1";
            this.tLAmplitude2.Caption = ResourceCulture.GetString("Amplitude") + " 2";
            this.tLTime2.Caption = ResourceCulture.GetString("Time") + " 2";
            this.tLSingleVolt.Caption = ResourceCulture.GetString("SinglecellVoltageStopCondition") + "(" + uOrder.Voltage + ")";
        }
        #endregion

    }
}
